#include "GameCore.h"

#include "../_Global/GlobalClass.h"


GameCore::GameCore(QObject *parent)
    :QThread(parent)
{
    //eGameType = Game_Type_NULL;
    //m_nPlayerCount = 0;
    //m_nMyID = -1;
    //m_bMaster = true;
    m_nGameRandom = -1;
    m_nGameMessageDelay = 0;

    m_eGameStatus = Game_Status_GameOver;
    m_bInitOnceInThread = false;
    m_bInitOnce = false;

    m_arrayPai = new _Pai[GAME_MAX_NUM_PAI];
    m_arrayPlayer = new _Player[GAME_MAX_NUM_PLAYER];        //玩家


    //初始化玩家
    int i;
    for(i = 0; i < GAME_MAX_NUM_PLAYER; i++)
    {
        m_arrayPlayer[i].setProp(i);
        m_listPlayer.append(m_arrayPlayer + i);
        connect(m_arrayPlayer + i, &_Player::s_Critical, this, &GameCore::s_Critical);
        //m_arrayPlayer[i].moveToThread(this);
    }

    for(i = 0; i < GAME_MAX_NUM_PAI; i++)
    {
        //m_arrayPai[i].iID = i;
        m_listPaiDui.append(m_arrayPai + i);
        //m_arrayPai[i].moveToThread(this);
    }


    //初始化所有牌
    int n = 0;
    i = 0;
    for(n += 20; i < n; i++)  //杀
    {
        m_arrayPai[i].setProp(i,_Pai::Value_Sha);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Sha);
    }
    for(n += 10; i < n; i++)  //闪
    {
        m_arrayPai[i].setProp(i,_Pai::Value_Shan);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Shan);
    }
    for(n += 6; i < n; i++) //桃
    {
        m_arrayPai[i].setProp(i,_Pai::Value_Tao);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Tao);
    }
    for(n += 4; i < n; i++)   //拆
    {
        m_arrayPai[i].setProp(i,_Pai::Value_Chai);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Chai);
    }
    for(n += 3; i < n; i++)   //顺
    {
        m_arrayPai[i].setProp(i,_Pai::Value_Shun);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Shun);
    }
    for(n += 3; i < n; i++)   //无中
    {
        m_arrayPai[i].setProp(i,_Pai::Value_WuZhong);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_WuZhong);
    }
    for(n += 2; i < n; i++)   //借刀
    {
        m_arrayPai[i].setProp(i,_Pai::Value_JieDao);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_JieDao);
    }
    for(n += 1; i < n; i++)   //万剑
    {
        m_arrayPai[i].setProp(i,_Pai::Value_WanJian);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_WanJian);
    }
    for(n += 2; i < n; i++)   //南蛮
    {
        m_arrayPai[i].setProp(i,_Pai::Value_NanMan);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_NanMan);
    }
    for(n += 9; i < n; i++)   //高考
    {
        m_arrayPai[i].setProp(i,_Pai::Value_GaoKao);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_GaoKao);
    }
    for(n += 1; i < n; i++)   //闪电
    {
        m_arrayPai[i].setProp(i,_Pai::Value_ShanDian);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_ShanDian);
    }
    for(n += 3; i < n; i++)   //乐
    {
        m_arrayPai[i].setProp(i,_Pai::Value_LeBuSiShu);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Le);
    }
    for(n += 2; i < n; i++)   //连弩
    {
        m_arrayPai[i].setProp(i,_Pai::Value_LianNu);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_LianNu);
    }
    for(n += 1; i < n; i++)   //丈八
    {
        m_arrayPai[i].setProp(i,_Pai::Value_ZhangBa);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_ZhangBa);
    }
    for(n += 1; i < n; i++)   //青龙
    {
        m_arrayPai[i].setProp(i,_Pai::Value_QingLong);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_QingLong);
    }
    for(n += 1; i < n; i++)   //贯石斧
    {
        m_arrayPai[i].setProp(i,_Pai::Value_GuanShiFu);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_GuanShiFu);
    }
    for(n += 1;i < n; i++)   //寒冰
    {
        m_arrayPai[i].setProp(i,_Pai::Value_HanBing);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_HanBingJian);
    }
    for(n += 2; i < n; i++)   //碳素笔
    {
        m_arrayPai[i].setProp(i,_Pai::Value_TanSu);
        //m_arrayPai[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_TanSu);
    }




    m_hashGameMessage[_Pai::Value_Sha].append(_T("请选择一名角色，作为%1的目标"));
    m_hashGameMessage[_Pai::Value_Sha].append(_T("请出一张%1"));
    m_hashGameMessage[_Pai::Value_Sha].append(_T("%1对%2使用%3"));
    m_hashGameMessage[_Pai::Value_Sha].append(_T("%1不出%2"));
    m_hashGameMessage[_Pai::Value_Sha].append(_T("%1出%2"));

    m_hashGameMessage[_Pai::Value_Shan].append(_T("请出%1张%2"));
    m_hashGameMessage[_Pai::Value_Shan].append(_T("%1不出%2"));
    m_hashGameMessage[_Pai::Value_Shan].append(_T("%1出%2"));

    m_hashGameMessage[_Pai::Value_Tao].append(_T("请出一张%1"));
    m_hashGameMessage[_Pai::Value_Tao].append(_T("%1使用%2"));
    m_hashGameMessage[_Pai::Value_Tao].append(_T("%1濒死，是否使用%2"));
    m_hashGameMessage[_Pai::Value_Tao].append(_T("%1不出%2"));
    m_hashGameMessage[_Pai::Value_Tao].append(_T("%1出%2"));

    m_hashGameMessage[_Pai::Value_Shun].append(_T("使用%1，请选择一名角色"));
    m_hashGameMessage[_Pai::Value_Shun].append(_T("%1，请选一张牌"));
    m_hashGameMessage[_Pai::Value_Shun].append(_T("%1对%2使用%3"));
    m_hashGameMessage[_Pai::Value_Shun].append(_T("%1借走%2的牌%3"));

    m_hashGameMessage[_Pai::Value_Chai].append(_T("使用%1，请选择一名角色"));
    m_hashGameMessage[_Pai::Value_Chai].append(_T("%1，请选一张牌"));
    m_hashGameMessage[_Pai::Value_Chai].append(_T("%1对%2使用%3"));
    m_hashGameMessage[_Pai::Value_Chai].append(_T("%1拆掉%2的%3"));

    m_hashGameMessage[_Pai::Value_WuZhong].append(_T("%1使用%2"));

    m_hashGameMessage[_Pai::Value_JieDao].append(_T("使用%1，请选择要借笔的目标"));
    m_hashGameMessage[_Pai::Value_JieDao].append(_T("请选择被%1的目标"));
    m_hashGameMessage[_Pai::Value_JieDao].append(_T("%1对%2使用%3，目标为%4"));
    m_hashGameMessage[_Pai::Value_JieDao].append(_T("%1借%2杀%3"));
    m_hashGameMessage[_Pai::Value_JieDao].append(_T("%1得到%2的%3"));

    m_hashGameMessage[_Pai::Value_GaoKao].append(_T("使用%1，请选择一名角色"));
    m_hashGameMessage[_Pai::Value_GaoKao].append(_T("%1对%2使用%3"));

    m_hashGameMessage[_Pai::Value_WanJian].append(_T("%1使用%2"));

    m_hashGameMessage[_Pai::Value_NanMan].append(_T("%1使用%2"));

    m_hashGameMessage[_Pai::Value_LeBuSiShu].append(_T("使用%1，请选择一名角色"));
    //m_hashGameMessage[_Pai::Value_LeBuSiShu].append(_T("%1判定结果%2，判定成功，"));
    m_hashGameMessage[_Pai::Value_LeBuSiShu].append(_T("%1对%2使用%3"));
    m_hashGameMessage[_Pai::Value_LeBuSiShu].append(_T("%1发动中"));
    m_hashGameMessage[_Pai::Value_LeBuSiShu].append(_T("%1回答错误，跳过出牌阶段"));

    m_hashGameMessage[_Pai::Value_ShanDian].append(_T("%1发动中"));
    m_hashGameMessage[_Pai::Value_ShanDian].append(_T("%1判定结果%2，判定成功"));
    //m_hashGameMessage[_Pai::Value_ShanDian].append(_T("回答错误,%1掉血1滴!"));
    m_hashGameMessage[_Pai::Value_ShanDian].append(_T("%1判定结果%2，判定失败"));
    m_hashGameMessage[_Pai::Value_ShanDian].append(_T("%1使用%2"));


    m_hashGameMessage[_Pai::Value_GuanShiFu].append(_T("您可以选择两张手牌弃掉，继续造成一点伤害"));
    m_hashGameMessage[_Pai::Value_GuanShiFu].append(_T("%1发动%2"));

    m_hashGameMessage[_Pai::Value_HanBing].append(_T("选择确定可以弃掉对方任意两张手牌，取消则继续造成伤害"));
    m_hashGameMessage[_Pai::Value_HanBing].append(_T("%1发动%2"));

    m_hashGameMessage[_Pai::Value_QingLong].append(_T("%1效果触发"));

    m_hashGameMessage[_Pai::Value_ZhangBa].append(_T("请选择任意两种手牌当%1使用"));

    m_hashGameMessage[_Pai::Value_TanSu].append(_T("请选择两张%1使用"));
    m_hashGameMessage[_Pai::Value_TanSu].append(_T("%1的%2效果触发"));

    m_hashGameMessage[_Pai::Value_All].append(_T("出牌阶段，请选择一张卡牌"));
    m_hashGameMessage[_Pai::Value_All].append(_T("您需要弃掉%1张卡牌，请选牌"));
    m_hashGameMessage[_Pai::Value_All].append(_T("%1摸%2张牌"));
    m_hashGameMessage[_Pai::Value_All].append(_T("%1掉%2血"));
    m_hashGameMessage[_Pai::Value_All].append(_T("%1离开考场"));
    m_hashGameMessage[_Pai::Value_All].append(_T("%1弃牌:%2"));
    m_hashGameMessage[_Pai::Value_All].append(_T("%1装备%2"));

    //m_hashGameMessage[_Pai::Value_All].append(_T("%1回答错误"));
    //m_hashGameMessage[_Pai::Value_All].append(_T("%1回答正确!"));
    //m_hashGameMessage[_Pai::Value_Sha].append();


}

GameCore::~GameCore()
{
    if(m_arrayPai != nullptr)
    {
        delete[] m_arrayPai;
        m_arrayPai = nullptr;
    }
    if(m_arrayPlayer != nullptr)
    {
        delete[] m_arrayPlayer;
        m_arrayPlayer = nullptr;
    }
}

int GameCore::InitOnce()
{
    if(m_bInitOnce)
    {
        return 1;
    }

    m_bInitOnce = true;
    emit s_InitOnce(GAME_MAX_NUM_PAI, GAME_MAX_NUM_PLAYER);
    qDebug() << "InitOnce over";
    return 0;
}

int GameCore::InitOnceInThread()
{
    if(m_bInitOnceInThread)
    {
        return 1;
    }

    m_bInitOnceInThread = true;
    qDebug() << "InitOnceInThread over";
    return 0;
}

int GameCore::GameInit(int sr)
{
    //Sleep(5000);
    emit s_Debug(QString("GameCore随机数种子:%1").arg(lmsSrand(sr)));
    m_eGameStatus = Game_Status_Continue;

//初始化,清空所有变量
    memset(m_arrayPaiDuiID,0,sizeof(m_arrayPaiDuiID));

    m_nGameRound = 0;
    //eWinner = Game_ShenFen_NULL;
    m_nWinner = -1;

    memset(&m_DianJiPai,0,sizeof(m_DianJiPai));
    memset(m_arrayStep,0,sizeof(m_arrayStep));
    m_nStepCount = -1;
    //shortMsg = "";
    m_strMsg = "";


    FlagMenus(_BaseObject::Flag_Null,_BaseObject::Flag_Null,_BaseObject::Flag_Null);



    m_nQiPaiDuiShu = GAME_MAX_NUM_PAI;
    m_nPaiDuiShu = 0;

    int i;
    for(i = 0; i < GAME_MAX_NUM_PAI; i++)    //清除状态
    {
        m_arrayPai[i].init();

        m_arrayPai[i].m_bUsing = true;
        m_arrayQiPaiDui[i] = m_arrayPai + i; //将所有牌放入弃牌堆
        //PaiDui[i]->iFlag = _BaseObject::flag_Show;
        //PaiDui[i]->setPosition(_BaseObject::Area_PaiDui,i,-1);
        //m_arrayPai[i].setFlags(_BaseObject::flag_Show);
    }
    for(i = 0; i < m_nPlayerCount; i++)    //清除武将数
    {
        m_arrayPlayer[i].init();
        m_arrayPlayer[i].m_bUsing = true;
    }
    for(; i < GAME_MAX_NUM_PLAYER; i++)    //清除武将数
    {
        m_arrayPlayer[i].init();
    }

    m_mapWuJiangUsingTimes.clear();




//根据游戏,进行初始化一些特定变量

    for(i = 0; i < m_nPlayerCount; i++)
    {
        emit s_Debug(tr("Player%1'JiaoSe is %2").arg(i).arg(m_arrayPlayer[i].m_eJiaoSe));
    }

    emit s_Debug(_T("玩家数(%1),m_nMyID(%2),房主(%3)")
            .arg(m_nPlayerCount)
            .arg(m_nMyID)
            .arg(m_bMaster));




    //要用到初始化后的数据,所以QML初始化放在后面!
    emit s_GameInitFinished(m_nPlayerCount,m_nMyID);


    emit s_RefreshAllPokePosition();
    emit s_RefreshAllFlags();

    return 0;
}

GameCore::_GameStatus GameCore::ChoiceWuJiang(int count)
{
    emit s_Message(_T("选择武将!"));

    emit s_ChoiceWuJiang();
    WaitForUserAction();

    return gameStatus();
}

GameCore::_GameStatus GameCore::GameBegin()
{
    //Sleep(1000);
    emit s_GameBegin();

    //!!!分配ID!!!
    //m_nMyID = rand() % m_nPlayerCount;
    //for(i = 0; i < jiaoSeList.count(); i++)
    //    m_arrayPlayer[i].m_eJiaoSe = jiaoSeList.at(i);



    //emit s_RefreshAllPokePosition();
    //emit s_RefreshAllFlags();


    emit s_Message(_T("<font size=10>-英语杀-</font>"));
    ShowChuPaiMessage(_T("游戏开始"), 0);

    //Sleep(m_ComputerDelay);

    if(ChoiceWuJiang(m_nPlayerCount) != Game_Status_Continue)
        return gameStatus();

    if(!Xipai())
    {
        emit s_Critical(_T("[!GameCore]GameBegin ERROR!"));
        return setGameStatus(Game_Status_ERROR);
    }
    if(gameStatus() != Game_Status_Continue)return gameStatus();


    //Player[m_nMyID].Kanshoupai();

    //cstr.Format(_T("牌堆还剩%d张牌！"),m_nPaiDuiShu);
    //emit s_Message(cstr);


    //每人四张牌
    for(int i = m_nGameRound;;)
    {
        if(!MoPai(i,4))
        {
            emit s_Critical(_T("[!GameCore]GameBegin ERROR!"));
            return setGameStatus(Game_Status_ERROR);
        }
        i = (i + 1) % m_nPlayerCount;
        if(i == m_nGameRound)
            break;
    }
    emit s_RefreshAllPokePosition();

    //m_arrayPlayer->getShouPai(m_arrayPai + 59);

    for(;;m_nGameRound ++) //游戏主循环
    {
        //lmsSrand();
        m_nGameRound = m_nGameRound % m_nPlayerCount;

        emit s_Debug(_T("iMain(%1)").arg(m_nGameRound));

        if(m_arrayPlayer[m_nGameRound].dead() == true)
            continue;
        if(m_arrayPlayer[m_nGameRound].m_bUsing == false)
            return setGameStatus(Game_Status_ERROR);

        m_arrayPlayer[m_nGameRound].m_nSha = 1;
        m_arrayPlayer[m_nGameRound].m_nJieDuan = 0;
        m_arrayPlayer[m_nGameRound].m_nChuPaiPoint = 0;

        InitJiNeng(m_nGameRound);


        //阶段:
        while(m_arrayPlayer[m_nGameRound].m_nJieDuan < 6)
        {
            if(m_arrayPlayer[m_nGameRound].dead() == true)
                break;
            switch(m_arrayPlayer[m_nGameRound].m_nJieDuan)
            {
            case 0:
                HuiHeKaiShi(m_nGameRound);
                //emit s_RefreshAllPokePosition();
                //emit s_RefreshAllFlags();
                break;
            case 1:
                PanDingJieDuan(m_nGameRound);
                break;
            case 2:
                MoPaiJieDuan(m_nGameRound);
                break;
            case 3:
                ChuPaiJieDuan(m_nGameRound);
                break;
            case 4:
                QiPaiJieDuan(m_nGameRound);
                break;
            case 5:
                HuiHeJieShu(m_nGameRound);
                break;
            default:
                break;
            }

            if(gameStatus() != Game_Status_Continue)return gameStatus();
            if(m_arrayPlayer[m_nGameRound].isComputer())
            {
                //Sleep(m_ComputerDelay);
            }
            if(m_arrayPlayer[m_nGameRound].dead() == true)   //如果玩家死亡
            {
                break;
            }
            m_arrayPlayer[m_nGameRound].m_nJieDuan++;
        }


        //str._Format(("-------弃牌堆：%d---------"),m_nQiPaiDuiShu);
        //emit s_Message(str);
    }

    return gameStatus();
}


int GameCore::GameOver()
{
    m_strMsg = QString("游戏正常结束:%1").arg(gameStatus());
    emit s_Message(m_strMsg);

    emit s_GameOver(gameStatus());
    //qDebug()<<pWordLib->rightCount()<<pWordLib->errCount();
    return 0;
}

bool GameCore::CreatePaiDuiID()
{
    //lmsSrand();

    if(!GetDifferentNumber(0,m_nQiPaiDuiShu,m_arrayPaiDuiID,m_nQiPaiDuiShu,sizeof(qint32)))
    {
        emit s_Critical(_T("[!GameCore]随机范围出错 ERROR!"));
        setGameStatus(Game_Status_ERROR);
        return false;
    }

    return true;
    /*
    //新算法!!!???改!!!
    for(i=0; i<POKE_MAXNUM; i++)    //!!!将 a 中的每个1~POKE_MAXNUM定位到x,y,z
    {
        x=(a[i]-1)/52;
        y=((a[i]-1)-52*x)/13;
        z=(a[i]-1)%13;
        paidui[i]=A[x][y][z];        //x,y,z对应一个a
    }*/
}

void GameCore::RefreshPaiDui()
{
    int i;
    for(i = 0; i < m_nQiPaiDuiShu; i++)    //按照m_arrayPaiDuiID将QiPaiDui中的牌放入PaiDui
    {
        m_arrayPaiDui[i] = m_arrayQiPaiDui[m_arrayPaiDuiID[i]];
        m_arrayQiPaiDui[m_arrayPaiDuiID[i]] = nullptr;
        //PaiDui[i]->setFlags(_BaseObject::flag_Show);
        //PaiDui[i]->moveTo(PAIDUI_X,PAIDUI_Y);
        m_arrayPaiDui[i]->setFlags(_BaseObject::Flag_Null);
        m_arrayPaiDui[i]->setPosition(_BaseObject::Area_PaiDui,i,-1);
    }
    m_nPaiDuiShu += m_nQiPaiDuiShu;
    m_nQiPaiDuiShu = 0;

    //剩下的牌
    for(i = m_nPaiDuiShu; i < GAME_MAX_NUM_PAI; i++)
        m_arrayPaiDui[i] = nullptr;

}

bool GameCore::Xipai()
{
    if(!CreatePaiDuiID())
    {
        emit s_Critical(_T("[!GameCore]XiPai ERROR!"));
        setGameStatus(Game_Status_ERROR);
        return false;
    }
    RefreshPaiDui();
    return true;
}

_Pai* GameCore::Mo1Pai()
{
    _Pai* p;
    if(m_nPaiDuiShu == 0)
    {
        if(!Xipai())
        {
            emit s_Critical(_T("[!GameCore]Mo1Pai ERROR!"));
            setGameStatus(Game_Status_ERROR);
            return nullptr;
        }
    }
    else if(m_nPaiDuiShu < 0)
    {
        emit s_Critical("[!GameCore]Mo1Pai ERROR:m_nPaiDuiShu < 0!");
        return nullptr;
    }
    p = m_arrayPaiDui[m_nPaiDuiShu - 1];        //取牌
    m_nPaiDuiShu--;
    //p->setPosition(ITEM_TYPE_NULL,-1,-1);
    return p;
}

bool GameCore::MoPai(int player,int n)
{
    m_strMsg = m_hashGameMessage[_Pai::Value_All][2]
            .arg(getPlayerShowName(player))
            .arg(n);
    emit s_Message(m_strMsg);

    //摸n牌
    int i;
    for(i = 0; i < n; i++)
    {
        _Pai *p = Mo1Pai();
        if(gameStatus() != Game_Status_Continue)return false;
        if(p == nullptr)
        {
            emit s_Critical(_T("[!GameCore]MoPai ERROR!"));
            setGameStatus(Game_Status_ERROR);
            return false;
        }
        if(m_arrayPlayer[player].getShouPai(p) < 0)
        {
            QiPai(p);
        }
    }
    return true;
}

GameCore::_GameStatus GameCore::DiaoXue(int n, int player, int player1, _Pai::_PaiValue v)
{
    m_strMsg = m_hashGameMessage[_Pai::Value_All][3]
        .arg(getPlayerShowName(player))
        .arg(n);
    emit s_Message(m_strMsg);

    m_arrayPlayer[player].addBlood(-n);
    //RefreshUI();

    if(m_arrayPlayer[player].m_nTiLi <= 0)   //player濒死
    {
        m_strMsg = _T("%1将要离开考场")
            .arg(getPlayerShowName(player));
        emit s_Message(m_strMsg);
        ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);

        int ret;
        int pp = m_nGameRound;

        for(;;)    //循环求桃
        {
            ret = QiuTao(player,pp);
            if(ret == Game_Button_OK)
            {
                if(ChiTao(player) > 0)
                    break;
                continue;
            }
            else if(ret == Game_Button_Cancel)    //pp不出桃
            {
                pp = GetNextPlayer(pp);
                if(pp == m_nGameRound)
                    break;
            }
            else
                return setGameStatus(Game_Status_ERROR);
        }


        /*
        if((*player1).juese)    //player1是玩家
        {
        for(;;)    //向player1求桃
        {
        printf("电脑濒死，是否使用%s？",PAI_103);
        i=QiuTao(player1,qipaidui,qipaishu);
        if(i==0)((*player2).tili)++;
        if((i==-1)||((*player2).tili>0))    //不出桃 或 体力大了
        break;
        }
        if((*player2).tili>0)
        return 0;
        else    //向player2求桃
        {
        for(;;)
        {
        i=QiuTao(player2,qipaidui,qipaishu);
        if(i==0)((*player2).tili)++;
        if((i==-1)||((*player2).tili>0))break;
        }
        if((*player2).tili>0)return 0;
        else return -1;
        }
        }
        else    //player1是电脑,player2玩家
        {
        for(;;)
        {
        printf("玩家濒死，是否使用%s？",PAI_103);
        i=QiuTao(player2,qipaidui,qipaishu);
        if(i==0)((*player2).tili)++;
        if((i==-1)||((*player2).tili>0))break;
        }
        if((*player2).tili>0)return 0;
        else return -1;
        }
        */
    }

    if(m_arrayPlayer[player].m_nTiLi <= 0)
        return SetPlayerDead(player);
    else
        return DiaoXueJiNeng(n,player,player1,v);
}

GameCore::_GameButtonValue GameCore::QiuTao(int player1,int player2)
{
    _GameButtonValue ret;
    for(;;)
    {
        //cstr.Format(_T("如果要使用%s请输入手牌之前的编号，不需要请输入“-1”，以回车结束！"),PAI_103);

        if(m_arrayPlayer[player2].isMe())
        {
            m_strMsg = _T("%1将要离开考场，是否使用%2")
                .arg(getPlayerShowName(player1))
                .arg(_Pai::getName(_Pai::Value_Tao));
            emit s_Message(m_strMsg);
            ShowChuPaiMessage(m_strMsg, 0);
        }

        ret = ChuPai(player2,GameCore::Game_ChuPai_Type_Pai, _Pai::Value_Tao, -1, -1, player1, 0, m_strMsg);
        if(ret == Game_Button_Cancel)
        {
            m_strMsg = m_hashGameMessage[_Pai::Value_Tao][3]
                .arg(getPlayerShowName(player2))
                .arg(_Pai::getName(_Pai::Value_Tao));
            emit s_Message(m_strMsg);
            ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
            return Game_Button_Cancel;
        }
        else if(ret == Game_Button_OK)        //桃
        {
            XiaoGuoQiPai(0);

            m_strMsg = m_hashGameMessage[_Pai::Value_Tao][4]
                .arg(m_arrayPlayer[player2].getWujiangData()->getName())
                .arg(_Pai::getName(_Pai::Value_Tao));
            emit s_Message(m_strMsg);
            ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
            return Game_Button_OK;
        }
        else
        {
            return ret;
        }
    }
}

//设置player死亡,弃牌,返回是否继续游戏(Game_Status_Gameover)
GameCore::_GameStatus GameCore::SetPlayerDead(int player)
{
    m_arrayPlayer[player].setDead(true);

    // +  +
    int i;
    for(i = 0; i < m_arrayPlayer[player].m_listShouPai.count(); i++)
    {
        _Pai *p = m_arrayPlayer[player].qiShouPai(i,false);
        if(p != nullptr)
        {
            QiPai(p);

            m_strMsg = m_hashGameMessage[_Pai::Value_All][5]
                    .arg(getPlayerShowName(player))
                    .arg(p->getName());
            emit s_Message(m_strMsg);
        }
    }
    m_arrayPlayer[player].tiaoZhengShouPaiDui();

    while(1)
    {
        _Pai *p = m_arrayPlayer[player].qiZhuangBei();
        if(p != nullptr)
        {
            QiPai(p);

            m_strMsg = m_hashGameMessage[_Pai::Value_All][5]
                .arg(getPlayerShowName(player))
                .arg(p->getName());
            emit s_Message(m_strMsg);
        }
        else
            break;
    }
    while(m_arrayPlayer[player].m_listPanDing.count() > 0)
    {
        _Pai *p = m_arrayPlayer[player].qiPanDing(0);
        if(p != nullptr)
        {
            QiPai(p);

            m_strMsg = m_hashGameMessage[_Pai::Value_All][5]
                .arg(getPlayerShowName(player))
                .arg(p->getName());;
            emit s_Message(m_strMsg);
        }
    }

    m_strMsg = m_hashGameMessage[_Pai::Value_All][4]
            .arg(getPlayerShowName(player));
    emit s_Message(m_strMsg);
    ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
    //m_arrayPlayer[player].ZhuangBeiCount = 0;//m_arrayPlayer[player].PanDingCount = m_arrayPlayer[player].ShouPaiCount = 0;
    /*
    for(i = 0; i < GAME_PLAYER_ZHUANGBEISHU; i++)
    {
        if(m_arrayPlayer[player].zhuangbei[i].pData == nullptr)
            continue;

        cstr.Format(_T("%s弃牌:%s"),
            m_arrayPlayer[player].strName,m_arrayPlayer[player].zhuangbei[i]);
        emit s_Message(cstr);
        QiPai(m_arrayPlayer[player].zhuangbei[i].QiPai());
        Player[player].zhuangbei[i].iValue = GAME_POKE_EMPTY;
    }

    for(i = 0; i < GAME_PLAYER_PANDINGSHU; i++)
    {
        if( m_arrayPlayer[player].pandingpai[i].leixing == GAME_POKE_EMPTY)
            continue;
        cstr.Format(_T("%s弃牌:%s"),
            Player[player].NameString(),Player[player].pandingpai[i].PaiString());
        emit s_Message(cstr);

        QiPai(Player[player].pandingpai[i]);
        Player[player].pandingpai[i].iValue = GAME_POKE_EMPTY;
    }
    if(player == m_nGameRound)
        setGameStatus(Game_Status_NextTurn);
    */


    emit s_RefreshAllPokePosition();
    emit s_RefreshAllFlags();

    //检测游戏是否结束
    return CheckGameOver();
}

GameCore::_GameStatus GameCore::CheckGameOver()
{
    if(gameStatus() != Game_Status_Continue)return gameStatus();


    int n = -1, i;
    for(i = 0; i < m_nPlayerCount; i++)
    {
        if(m_arrayPlayer[i].dead() == false)
        {
            if(-1 == n)
            {
                n = m_arrayPlayer[i].m_nDuiWu; //第一次时,保存队伍
                continue;
            }
            //else if(-1 == n)    //存在1个-1人 和 其他人
            //    return m_eGameStatus;
            else if(n == m_arrayPlayer[i].m_nDuiWu)    //一个队伍的
                continue;
            else    //不是1个队伍的
                return gameStatus();
        }
    }
    m_nWinner = n;

    return setGameStatus(Game_Status_GameOver);
    /*
        if(m_eGameStatus == Game_Status_NextTurn)
            return Game_Status_NextTurn;
        else
            return gameStatus();
    */
}

GameCore::_GameStatus GameCore::setGameStatus(GameCore::_GameStatus status)
{
    if(m_eGameStatus == Game_Status_Continue && status != Game_Status_Continue)
    {
        m_eGameStatus = status;
        emit s_Debug(_T("[Game]游戏将要结束:状态码:%1").arg(m_eGameStatus));
    }
    return m_eGameStatus;
}

int GameCore::ShaiDian()
{
    return (int)Random(1,7);
}


//!!!只用了 技能的 第一个_JiNeng
void GameCore::InitJiNeng(int player)
{
    QObjectList::const_iterator iiJiNengPai;
    for (iiJiNengPai = m_arrayPlayer[player].m_listJiNengPai.constBegin(); iiJiNengPai != m_arrayPlayer[player].m_listJiNengPai.constEnd(); ++iiJiNengPai)
    {
        _Pai *pJiNeng = (_Pai*)(*iiJiNengPai);
        switch (pJiNeng->m_listJiNeng.at(0)->m_eJiNengType)
        {
        case _JiNeng::JiNengType_PuTongJi:
            pJiNeng->m_listJiNeng.at(0)->m_nJiNengParam = pJiNeng->m_listJiNeng.at(0)->m_nJiNengParam2;
            break;
        default:
            break;
        }
    }
}

GameCore::_GameStatus GameCore::HuiHeKaiShi(int player)
{
    if(m_arrayPlayer[player].dead())
        return gameStatus();

    switch(m_arrayPlayer[player].m_nHuiHeKaiShi)
    {
    case 0:
        m_strMsg = _T("%1回合开始阶段")
                .arg(getPlayerShowName(player));
        emit s_Message(m_strMsg);
        ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);

        HuiHeKaiShiChuLi(player);
        if(gameStatus() != Game_Status_Continue)return gameStatus();
        break;
    case -1:
        break;
    default:
        emit s_Critical(_T("[!GameCore]回合开始阶段参数 ERROR!"));
        return setGameStatus(Game_Status_ERROR);
    }
    return gameStatus();
}

GameCore::_GameStatus GameCore::PanDingJieDuan(int player)
{
    if(m_arrayPlayer[player].dead())
        return gameStatus();

    switch(m_arrayPlayer[player].m_nPanDingJieDuan)
    {
    case 0:
        m_strMsg = _T("%1判定阶段")
                .arg(getPlayerShowName(player));
        emit s_Message(m_strMsg);
        ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);

        PanDingChuLi(player);
        if(gameStatus() != Game_Status_Continue)return gameStatus();
        break;
    case -1:
        break;
    default:
        emit s_Critical(_T("[!GameCore]判定阶段参数 ERROR!"));
        return setGameStatus(Game_Status_ERROR);
    }

    //else if(Player[player].PanDingShu() == 0);
    //else emit s_Message(_T("判定阶段参数错误！"));

    return gameStatus();
}

GameCore::_GameStatus GameCore::MoPaiJieDuan(int player)
{
    if(m_arrayPlayer[player].dead())
        return gameStatus();

    switch(m_arrayPlayer[player].m_nMoPaiJieDuan)
    {
    case 0:
        MoPaiChuLi(player);

        m_strMsg = _T("%1摸牌阶段")
                .arg(getPlayerShowName(player));
        emit s_Message(m_strMsg);
        ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);

        if(gameStatus() != Game_Status_Continue)return gameStatus();
        break;
    case -1:
        break;
    default:
        emit s_Critical(_T("[!GameCore]摸牌阶段参数 ERROR!"));
        return setGameStatus(Game_Status_ERROR);
    }

    return gameStatus();

}

GameCore::_GameStatus GameCore::ChuPaiJieDuan(int player)
{
    if(m_arrayPlayer[player].dead())
        return gameStatus();

    switch(m_arrayPlayer[player].m_nChuPaiJieDuan)    //出牌 为0可以出牌,-1不可以出牌
    {
    case 0:
        if(!m_arrayPlayer[player].isMe())
        {
            m_strMsg = _T("%1出牌阶段")
                    .arg(getPlayerShowName(player));
            emit s_Message(m_strMsg);
            ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
        }

        ChuPaiChuLi(player);
        if(gameStatus() != Game_Status_Continue)return gameStatus();
        break;
    case -1:
        m_arrayPlayer[player].m_nChuPaiJieDuan = 0;
        break;
    default:
        emit s_Critical(_T("[!GameCore]出牌阶段参数 ERROR!"));
        return setGameStatus(Game_Status_ERROR);
    }

    return gameStatus();
}

GameCore::_GameStatus GameCore::QiPaiJieDuan(int player)
{
    if(m_arrayPlayer[player].dead())
        return gameStatus();

    switch(m_arrayPlayer[player].m_nQiPaiJieDuan)
    {
    case 0:
        m_strMsg = _T("%1弃牌阶段")
                .arg(getPlayerShowName(player));
        emit s_Message(m_strMsg);
        if(!m_arrayPlayer[player].isMe())     //如果是本人,则不显示 弃牌阶段 字
        {
            ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
        }

        QiPaiChuLi(player);
        if(gameStatus() != Game_Status_Continue)return gameStatus();
        break;
    case -1:
        break;
    default:
        emit s_Critical(_T("[!GameCore]弃牌阶段参数 ERROR!"));
        return setGameStatus(Game_Status_ERROR);
    }

    return gameStatus();

}


GameCore::_GameStatus GameCore::HuiHeJieShu(int player)
{
    if(m_arrayPlayer[player].dead())
        return gameStatus();

    switch(m_arrayPlayer[player].m_nHuiHeJieShuJieDuan)
    {
    case 0:
        m_strMsg = _T("%1回合结束阶段")
                .arg(getPlayerShowName(player));
        emit s_Message(m_strMsg);
        ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);

        HuiHeJieShuChuLi(player);
        if(gameStatus() != Game_Status_Continue)return gameStatus();
        break;
    case -1:
        break;
    default:
        emit s_Critical(_T("[!GameCore]回合结束阶段参数 ERROR!"));
        return setGameStatus(Game_Status_ERROR);
    }

    return gameStatus();
}


int GameCore::QiPai(_Pai *p)
{
    if(p != nullptr)
    {
        m_arrayQiPaiDui[m_nQiPaiDuiShu] = p;
        //p->setFlags(_Pai::flag_ZhengMian & _Pai::flag_Show);
        p->setFlags(_BaseObject::Flag_Null);
        p->setPosition(_BaseObject::Area_QiPaiDui,m_nQiPaiDuiShu,-1);
        m_nQiPaiDuiShu++;
    }
    else
    {
        emit s_Critical(_T("[!GameCore]弃牌 ERROR 1!"));
        setGameStatus(Game_Status_ERROR);
        return -1;
    }
    if(m_nQiPaiDuiShu > GAME_MAX_NUM_PAI)
    {
        emit s_Critical(_T("[!GameCore]弃牌 ERROR 2!"));
        setGameStatus(Game_Status_ERROR);
    }

    return m_nQiPaiDuiShu;
}

int GameCore::GetNextPlayer(int player)
{
    //int player = w;
    for(;;)
    {
        player ++;
        if(player < 0)player = 0;
        player = player % m_nPlayerCount;
        if(m_arrayPlayer[player].dead() == false)
            return player;
    }
}


bool GameCore::ComputeDistent(int player1,int player2,int type)
{
    //计算player1到player2的实际距离到 j1
    int j1 = 1, j2 = 1;

    int i = GetNextPlayer(player1);
    for(; i != player2; i = GetNextPlayer(i))
    {
        j1++;
    }

    i = GetNextPlayer(player2);
    for(; i != player1; i = GetNextPlayer(i))
    {
        j2++;
    }

    if(j1 > j2)
        j1 = j2;




    //计算 武器的 攻击距离的技能数
    QList<_JiNeng*>::const_iterator iiJiNeng;
    int gongjijuli = 0;
    _Pai* p = m_arrayPlayer[player1].getZhuangBeiData(_BaseObject::Area_WuQi);
    if(p != nullptr)
    {
        for(iiJiNeng = p->m_listJiNeng.constBegin(); iiJiNeng != p->m_listJiNeng.constEnd(); iiJiNeng++)
        {
            if((*iiJiNeng)->m_eJiNengValue == _JiNeng::JiNengValue_GongJiJuLi)
                gongjijuli += (*iiJiNeng)->m_nJiNengParam;
        }
    }

    //计算 玩家的 拉近距离的技能数
    QObjectList::const_iterator iiJiNengPai;
    int jinshenjuli = 1;
    for(iiJiNengPai = m_arrayPlayer[player1].m_listJiNengPai.constBegin(); iiJiNengPai != m_arrayPlayer[player1].m_listJiNengPai.constEnd(); iiJiNengPai++)
    {
        _Pai* pJiNengPai = (_Pai*)(*iiJiNengPai);

        for(iiJiNeng = pJiNengPai->m_listJiNeng.constBegin(); iiJiNeng != pJiNengPai->m_listJiNeng.constEnd(); iiJiNeng++)
        {
            if((*iiJiNeng)->m_eJiNengValue == _JiNeng::JiNengValue_JianJuLi)
                jinshenjuli += (*iiJiNeng)->m_nJiNengParam;
        }

    }



    switch(type)
    {
    case 0:
    case 1:
        return j1 <= jinshenjuli;
        break;
    case 2:
        return j1 <= jinshenjuli + gongjijuli;
        break;
    default:
        break;
    }

    return false;
}


void GameCore::FlagMenus(_BaseObject::_Flags ok, _BaseObject::_Flags cancel, _BaseObject::_Flags giveup)
{
    m_flagsButton[Game_Button_OK] = ok;
    m_flagsButton[Game_Button_Cancel] = cancel;
    m_flagsButton[Game_Button_Giveup] = giveup;
    //qDebug()<<"C flags Menus!!!"<<ok<<cancel<<giveup;
    //QVariant vOK,vCancel,vGiveup;
    //vOK = QVariant::fromValue(ok);
    //vCancel = QVariant::fromValue(cancel);
    //vGiveup = QVariant::fromValue(giveup);
    //emit s_FlagMenus2(QVariant::fromValue(ok),QVariant::fromValue(cancel),QVariant::fromValue(giveup));
    //emit s_FlagMenus1(ok,cancel,giveup);
    emit s_FlagMenus(ok,cancel,giveup);
}

int GameCore::ResetAllPlayers(int player)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        m_arrayPlayer[i].resetAllFlags();
        nCount ++;
    }
    while(i = GetNextPlayer(i),i != player);


    return nCount;
}

int GameCore::FlagAllPlayers(_BaseObject::_Flags eFlags, _BaseObject::_FlagType type, int player)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        m_arrayPlayer[i].setFlags(eFlags,type);
        nCount ++;
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

/*
int GameCore::FlagAllPlayersByPaiCount(_BaseObject::_Flags flags, _Pai::_PaiValue paiValue, int player, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_arrayPlayer[i].dead() == true)continue;
        if(ignoreEnable == 1 && m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable)
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            continue;
        }
        if(m_arrayPlayer[i].getPaiCount(pandingValue, _BaseObject::Area_PanDing) > 0)
        {
            if(!test)m_arrayPlayer[i].setFlags(flags);
            nCount ++;
        }
        //else
        //{
        //    if(!test)m_arrayPlayer[i].clearSomeFlags(flags);
        //}
        //if(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable)  //上面不一定是设置enable
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}
*/
int GameCore::FlagAllPlayersByPanDing(_BaseObject::_Flags flags, _Pai::_PaiValue pandingValue, int player, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_arrayPlayer[i].dead() == true)continue;
        if(ignoreEnable == 1 && m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable)
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            continue;
        }
        if(m_arrayPlayer[i].getPaiCount(pandingValue, _BaseObject::Area_PanDing) > 0)
        {
            if(!test)m_arrayPlayer[i].setFlags(flags);
            nCount ++;
        }
        //else
        //{
        //    if(!test)m_arrayPlayer[i].clearSomeFlags(flags);
        //}
        //if(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable)  //上面不一定是设置enable
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

int GameCore::FlagAllPlayersByPaiCount(_BaseObject::_Flags flags, int compareType, int count, int player, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_arrayPlayer[i].dead() == true)continue;
        if(ignoreEnable == 1 && m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable)
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            continue;
        }

        if(compareType < 0 && m_arrayPlayer[i].getPaiCount() < count)
        {
            if(!test)m_arrayPlayer[i].setFlags(flags);
            nCount ++;
        }
        else if(compareType == 0 && m_arrayPlayer[i].getPaiCount() == count)
        {
            if(!test)m_arrayPlayer[i].setFlags(flags);
            nCount ++;
        }
        else if(compareType > 0 && m_arrayPlayer[i].getPaiCount() > count)
        {
            if(!test)m_arrayPlayer[i].setFlags(flags);
            nCount ++;
        }
        //else
        //{
        //    if(!test)m_arrayPlayer[i].clearSomeFlags(flags);
        //}
        //if(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable)  //上面不一定是设置enable
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

int GameCore::FlagAllPlayersWeapon(_BaseObject::_Flags eFlags, _BaseObject::_Areas areas, _BaseObject::_FlagType type, int player)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        nCount += m_arrayPlayer[i].flagZhuangBeiByArea(eFlags,areas,type);
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}


int GameCore::EnableAllPlayersByDistent(int player, int type, bool test, int ignoreEnable)
{
    int nCount = 0;

    if(!CheckPlayerPlay(player,false))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);   //设置player为完成标记
    do
    {
        if(ignoreEnable == 1 && (m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            continue;
        }

        if(ComputeDistent(player,i,type))
        {
            if(!test)m_arrayPlayer[i].setSomeFlags(_BaseObject::Flag_Enable);
            nCount ++;
        }
        else
        {
            if(!test)m_arrayPlayer[i].clearSomeFlags(_BaseObject::Flag_Enable);
        }
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}


int GameCore::EnableAllPlayersByZhuangBei(int player, _Pai::_PaiValue value, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_arrayPlayer[i].dead() == true)continue;
        //if(m_arrayPlayer[i].getZhuangBeiData(_BaseObject::Area_WuQi) == nullptr)continue;
        if(ignoreEnable == 1 && (m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            continue;
        }
        if(m_arrayPlayer[i].containZhuangBei(value))
        {
            if(!test)m_arrayPlayer[i].setSomeFlags(_BaseObject::Flag_Enable);
            nCount ++;
        }
        else
        {
            if(!test)m_arrayPlayer[i].clearSomeFlags(_BaseObject::Flag_Enable);
        }
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

int GameCore::EnableAllPlayersByPanDing(int player, _Pai::_PaiValue value, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_arrayPlayer[i].dead() == true)continue;
        if(ignoreEnable == 1 && (m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            continue;
        }
        if(m_arrayPlayer[i].containPanDing(value))
        {
            if(!test)m_arrayPlayer[i].setSomeFlags(_BaseObject::Flag_Enable);
            nCount ++;
        }
        else
        {
            if(!test)m_arrayPlayer[i].clearSomeFlags(_BaseObject::Flag_Enable);
        }
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

int GameCore::EnableAllPlayersByShouPai(int player, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
    {
        setGameStatus(Game_Status_ERROR);
        return -1;
    }

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_arrayPlayer[i].dead() == true)continue;
        if(ignoreEnable == 1 && m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable)
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable))
        {
            continue;
        }
        if(m_arrayPlayer[i].getPaiCount(_Pai::Value_All,_BaseObject::Area_ShouPai) > 0)
        {
            if(!test)m_arrayPlayer[i].setSomeFlags(_BaseObject::Flag_Enable);
            nCount ++;
        }
        else
        {
            if(!test)m_arrayPlayer[i].clearSomeFlags(_BaseObject::Flag_Enable);
        }
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

void GameCore::run()
{
    /* 线程的相关代码 */
    //pGame->m_pGameRes->m_WuJiang[0].setPixmap(QPixmap(":/Images/Pai/Sha"));

    //m_mutex.tryLock();      //不加这句就会出错!不知道为何
    //for(int i = 0; i < GAME_MAX_NUM_PAI; i++)
    //    emit s_Debug(_T(m_arrayPaiDuiID[i]));
    InitOnceInThread();
    if(GameInit(m_nGameRandom) < 0)
    {
        emit s_Critical(_T("[!GameCore]游戏初始化 ERROR!"));
        return;
    }
    GameBegin();
    GameOver();
}

